package com.zt.springcloud.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.zt.springcloud.entity.Order;
import com.zt.springcloud.entity.Product;
import com.zt.springcloud.entity.vo.OrderListVo;
import com.zt.springcloud.entity.vo.OrderVo;
import com.zt.springcloud.entity.vo.ProductVo;
import com.zt.springcloud.mapper.OrderMapper;
import com.zt.springcloud.mapper.ProductMapper;
import com.zt.springcloud.mapper.ShopCartMapper;
import com.zt.springcloud.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author RedReins
 * @version 1.0
 * @description: TODO
 * @date 2021/9/11 18:36
 */


@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private ProductMapper productMapper;


    @Override
    @Transactional
    public Boolean createOrder(String reqData,Long userId) {
        /*
        * 1.插入订单表 2.删除购物车
        * */
        OrderVo orderVo = JSONObject.parseObject(reqData, OrderVo.class);
        /*
        * 创建order对象
        * */
        Order order = new Order();
        String orderId = UUID.randomUUID().toString();
        order.setOrderId(orderId);
        order.setUserId(userId);
        order.setAddressId(orderVo.getAddressId());
        order.setOrderCount(orderVo.getOrderCount());
        order.setOrderPrice(orderVo.getOrderPrice());
        order.setPayMethod(orderVo.getPayMethod());

        /*
        * 插入数据库中
        * */
        orderMapper.createOrder(order);

        /*
        * 添加订单商品
        * */
        List<ProductVo> productList = orderVo.getProductList();
        productList.stream().forEach(item ->{
            Long itemId = item.getItemId();
            String delivery = item.getDelivery();
            Integer amount = item.getAmount();
            BigDecimal price = item.getPrice().multiply(BigDecimal.valueOf(amount));
            orderMapper.addOrderItem(orderId, itemId,amount,price,delivery);
            /*
             * 删除购物车中的商品
             * */
            shopCartMapper.removeCartItem(userId,itemId);
        });
        return true;
    }

    @Override
    @Transactional
    public List<OrderListVo> getUserAllOrder(Long userId) {
        /*
         * 根据用户id查询订单编号
         * */
        List<String> orderId = orderMapper.getOrderId(userId);
        List<OrderListVo> orderListVos = new ArrayList<>();
        for (String o : orderId) {
            OrderListVo orderListVo = new OrderListVo();
            /*
             * 订单号时间
             * */
            String date = orderMapper.selectOrderCreateTime(o);
            orderListVo.setOrderId(o);
            orderListVo.setCreateTime(date);
            List<ProductVo> allOrderItemsById = orderMapper.getAllOrderItemsById(o);
            allOrderItemsById.stream().forEach(item -> {
                Long itemId = item.getItemId();
                Product itemInfo = productMapper.getItemInfo(itemId);
                item.setImageUrl(itemInfo.getImageUrl());
                item.setTitle(itemInfo.getTitle());
            });
            orderListVo.setProductList(allOrderItemsById);
            orderListVos.add(orderListVo);
        }
        return orderListVos;
    }

    @Override
    public Boolean deleteOrderItem(String orderId) {
        Boolean aBoolean = orderMapper.deleteItemOrder(orderId);
        Boolean aBoolean1 = orderMapper.deleteItemOrderItems(orderId);
        return aBoolean1&&aBoolean;
    }
}
